<?php

	// TITLE: State AUTO MODEL
	// FILE: scaffolding/model/state.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createState = function($con, $state) {
		$sql = 'INSERT INTO state (id, abbreviation, title) VALUES (';
		$sql .= $state->getId() . ', ';
		$sql .= $state->getAbbreviation() . ', ';
		$sql .= $state->getTitle() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readState = function($con, $id) {
		$sql = 'SELECT * FROM state WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$state = new State();
			$state->setId(result[0]);
			$state->setAbbreviation(result[1]);
			$state->setTitle(result[2]);
		} else {
			$state = false;
		}
		return $state;
	};

	$updateState = function($con, $state) {
		$sql = 'UPDATE state SET ';
		$sql .= 'id = ' . $state->getId() . ', ';
		$sql .= 'abbreviation = ' . $state->getAbbreviation() . ', ';
		$sql .= 'title = ' . $state->getTitle() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deleteState = function($con, $id) {
		$sql = 'DELETE FROM state WHERE id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listState = function($con) {
		$sql = 'SELECT * FROM state';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$states = array();
			while ($result = mysqli_fetch_array($results)) {
				$state = new State();
				$state->setId(result[0]);
				$state->setAbbreviation(result[1]);
				$state->setTitle(result[2]);
				$states[] = state;
			}// end while
		} else {
			$states = false;
		}
		return $states;
	};

